package com.goodving.guli.bt.profile.scale;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;

import com.goodving.guli.bt.BLEService;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
 * Created by john on 2015/11/25.
 */
public class AbsGattCallback extends BluetoothGattCallback { // C3868a
    private static final String TAG = "AbsGattCallback";
    private static final String f19491b;
    private static final String f19492c;
    private static final String f19493d;
    private static final long GATT_OPERATION_TIMEOUT = 30;
    private final Map<BluetoothGattCharacteristic, INotifyCallback> m_NotifyCBs;
    private boolean m_isClean;
    private HandlerThread c_GattQueueThread;
    private Handler c_GattQueue;
    private final ReentrantLock c_GattOperationLock;
    private final Condition c_condGattOperationComplete;
    private boolean c_isGattOperationLocking;
    private int f19502o;
    private final Object f19503p;
    private boolean c_isRSSILocking;
    private int f19505r;
    private int f19506s;
    private final Context f19507t;
    private final BluetoothDevice f19508u;
    private int f19509v;
    private Runnable f19510w;
    private final ExecutorService f19511x;
    private BluetoothGatt f19512y;
    private XMBLEMSTATUS mConState;

    static {
        f19491b = AbsGattCallback.class.getSimpleName();
        f19492c = f19491b + "*";
        f19493d = f19491b;
    }

    class C3870c implements Runnable {
        final /* synthetic */ C3869b f19515a;

        C3870c(C3869b c3869b) {
            this.f19515a = c3869b;
        }

        public void run() {
            this.f19515a.f19514a.GattCB_onConnectionFailed();
        }
    }

    class C3869b implements Runnable {
        final /* synthetic */ AbsGattCallback f19514a;

        C3869b(AbsGattCallback c3868a) {
            this.f19514a = c3868a;
        }

        public void run() {
            this.f19514a.mConState = XMBLEMSTATUS.DISCONNECTED;
            GuliLog.ERROR("=================================================");
            GuliLog.ERROR("=============== CONNECT TIMEOUT ===============");
            GuliLog.ERROR("=================================================");
            this.f19514a.f19511x.execute(new C3870c(this));
        }
    }

    class C3882o implements Thread.UncaughtExceptionHandler {
        final /* synthetic */ C3881n f19541a;

        C3882o(C3881n c3881n) {
            this.f19541a = c3881n;
        }

        public void uncaughtException(Thread thread, Throwable th) {
            GuliLog.m12371e();
            th.printStackTrace();
        }
    }

    class C3881n implements ThreadFactory {
        final /* synthetic */ AbsGattCallback f19540a;

        C3881n(AbsGattCallback c3868a) {
            this.f19540a = c3868a;
        }

        public Thread newThread(Runnable runnable) {
            GuliLog.m12371e();
            GuliLog.INFO("********************************");
            GuliLog.INFO("NEW THREAD: " + AbsGattCallback.f19493d);
            GuliLog.INFO("********************************");
            Thread thread = new Thread(runnable, AbsGattCallback.f19493d);
            thread.setUncaughtExceptionHandler(new C3882o(this));
            return thread;
        }
    }

    class C3877j implements Thread.UncaughtExceptionHandler {
        final /* synthetic */ AbsGattCallback f19529a;

        C3877j(AbsGattCallback c3868a) {
            this.f19529a = c3868a;
        }

        public void uncaughtException(Thread thread, Throwable th) {
            GuliLog.m12371e();
            th.printStackTrace();
        }
    }

    protected AbsGattCallback(Context context, BluetoothDevice bluetoothDevice) {
        this.c_GattOperationLock = new ReentrantLock();
        this.c_condGattOperationComplete = this.c_GattOperationLock.newCondition();
        this.c_isGattOperationLocking = false;
        this.f19502o = C3867z.f19489j;
        this.f19503p = new Object();
        this.c_isRSSILocking = false;
        this.f19505r = 0;
        this.f19506s = C3867z.f19489j;
        this.f19509v = 45000;
        this.f19510w = new C3869b(this);
        this.f19511x = Executors.newSingleThreadExecutor(new C3881n(this));
        this.f19512y = null;
        this.mConState = XMBLEMSTATUS.DISCONNECTED;
        this.m_NotifyCBs = new HashMap();
        this.m_isClean = true;
        GuliLog.m12371e();
        this.c_GattQueueThread = new HandlerThread(f19492c);
        GuliLog.INFO("********************************");
        GuliLog.INFO("NEW THREAD: " + f19492c);
        GuliLog.INFO("********************************");
        this.c_GattQueueThread.setUncaughtExceptionHandler(new C3877j(this));
        this.c_GattQueueThread.start();
        this.c_GattQueue = new Handler(this.c_GattQueueThread.getLooper());
        this.f19507t = context;
        this.f19508u = bluetoothDevice;
    }

    class C3878k implements Runnable {
        final /* synthetic */ BluetoothGatt f19530a;
        final /* synthetic */ BluetoothGattCharacteristic f19531b;
        final /* synthetic */ AbsGattCallback f19532c;

        C3878k(AbsGattCallback c3868a, BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            this.f19532c = c3868a;
            this.f19530a = bluetoothGatt;
            this.f19531b = bluetoothGattCharacteristic;
        }

        public void run() {
            GuliLog.m12371e();
            GuliLog.ASSERTION_RUN_ON_THREAD(this.f19532c.c_GattQueueThread); //TBD f19536d ->f19532c
            GuliLog.ASSERT_NOT_NULL(this.f19530a);
            if (this.f19530a == null) {
                this.f19532c.c_GattOperationLock.lock();
                try {
                    this.f19532c.f19502o = C3867z.f19487h;
                    GuliLog.m12359b("NOTIFY: synchronizedReadCharacteristic(): " + this.f19532c.f19502o); //TBD f19536d ->f19532c
                    this.f19532c.c_isGattOperationLocking = false;
                    this.f19532c.c_condGattOperationComplete.signal();
                } finally {
                    this.f19532c.c_GattOperationLock.unlock();
                }
            } else {
                boolean readCharacteristic = this.f19530a.readCharacteristic(this.f19531b);
                GuliLog.ASSERTION_TRUE(readCharacteristic);
                if (!readCharacteristic) {
                    this.f19532c.c_GattOperationLock.lock();
                    try {
                        this.f19532c.f19502o = C3867z.f19488i;
                        GuliLog.m12359b("NOTIFY: synchronizedReadCharacteristic(): " + this.f19532c.f19502o);
                        this.f19532c.c_isGattOperationLocking = false;
                        this.f19532c.c_condGattOperationComplete.signal();
                    } finally {
                        this.f19532c.c_GattOperationLock.unlock();
                    }
                }
            }
        }
    }

    private synchronized int synchronizedReadCharacteristic(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) { // m25245a
        GuliLog.m12371e();
        GuliLog.m12350a("gatt=" + bluetoothGatt + ", characteristic=" + bluetoothGattCharacteristic);
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGatt);
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGattCharacteristic);

        Runnable c3878k = new C3878k(this, bluetoothGatt, bluetoothGattCharacteristic);
        this.c_GattOperationLock.lock();
        try {
            this.c_isGattOperationLocking = true;
            this.c_GattQueue.post(c3878k);
            while (this.c_isGattOperationLocking) {
                if (!isConnected(bluetoothGatt)) {
                    GuliLog.WARN("NO CONNECTION");
                    break;
                }
                GuliLog.m12359b("WAIT: synchronizedReadCharacteristic(): " + GattUtils.m25341c(bluetoothGattCharacteristic.getUuid()));
                this.c_condGattOperationComplete.await(GATT_OPERATION_TIMEOUT, TimeUnit.SECONDS);
                if (this.c_isGattOperationLocking) {
                    GuliLog.WARN("GATT OPERATION TIMEOUT");
                    GuliLog.m12359b("CONTINUE: synchronizedReadCharacteristic(): " + GattUtils.m25341c(bluetoothGattCharacteristic.getUuid()));
                    break;
                }
                GuliLog.m12359b("CONTINUE: synchronizedReadCharacteristic(): " + GattUtils.m25341c(bluetoothGattCharacteristic.getUuid()));
            }
            this.c_GattOperationLock.unlock();
        } catch (InterruptedException e) {
            e.printStackTrace();
            this.c_GattOperationLock.unlock();
        } catch (Throwable th) {
            this.c_GattOperationLock.unlock();
        }
        return this.f19502o;
    }


    class C3879l implements Runnable {
        final /* synthetic */ BluetoothGatt f19533a;
        final /* synthetic */ BluetoothGattCharacteristic f19534b;
        final /* synthetic */ byte[] f19535c;
        final /* synthetic */ AbsGattCallback f19536d;

        C3879l(AbsGattCallback c3868a, BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) {
            this.f19536d = c3868a;
            this.f19533a = bluetoothGatt;
            this.f19534b = bluetoothGattCharacteristic;
            this.f19535c = bArr;
        }

        public void run() {

            GuliLog.m12371e();
            GuliLog.ASSERTION_RUN_ON_THREAD(this.f19536d.c_GattQueueThread);
            GuliLog.ASSERT_NOT_NULL(this.f19533a);
            if (this.f19533a == null) {
                this.f19536d.c_GattOperationLock.lock(); //TBD f19532c ->f19536d
                try {
                    this.f19536d.f19502o = C3867z.f19487h; //TBD f19532c ->f19536d
                    GuliLog.m12359b("NOTIFY: synchronizedWriteCharacteristic(): " + this.f19536d.f19502o);
                    this.f19536d.c_isGattOperationLocking = false;
                    this.f19536d.c_condGattOperationComplete.signal();
                } finally {
                    this.f19536d.c_GattOperationLock.unlock();
                }
            } else {
                this.f19534b.setValue(this.f19535c);
                boolean writeCharacteristic = this.f19533a.writeCharacteristic(this.f19534b);
                GuliLog.ASSERTION_TRUE(writeCharacteristic);
                if (!writeCharacteristic) {
                    this.f19536d.c_GattOperationLock.lock();
                    try {
                        this.f19536d.f19502o = C3867z.f19488i;
                        GuliLog.m12359b("NOTIFY: synchronizedWriteCharacteristic(): " + this.f19536d.f19502o);
                        this.f19536d.c_isGattOperationLocking = false;
                        this.f19536d.c_condGattOperationComplete.signal();
                    } finally {
                        this.f19536d.c_GattOperationLock.unlock();
                    }
                }
            }
        }
    }

    private synchronized int synchronizedWriteCharacteristic(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) { // m25246a
        GuliLog.m12371e();
        GuliLog.m12350a("gatt=" + bluetoothGatt + ", characteristic=" + bluetoothGattCharacteristic + ", value=" + bArr);
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGatt);
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGattCharacteristic);
        GuliLog.ASSERT_NOT_NULL((Object) bArr);
        Runnable c3879l = new C3879l(this, bluetoothGatt, bluetoothGattCharacteristic, bArr);
        this.c_GattOperationLock.lock();
        try {
            this.c_isGattOperationLocking = true;
            this.c_GattQueue.post(c3879l);
            while (this.c_isGattOperationLocking) {
                if (!isConnected(bluetoothGatt)) {
                    GuliLog.WARN("NO CONNECTION");
                    break;
                }
                GuliLog.m12359b("WAIT: synchronizedWriteCharacteristic(): " + GattUtils.m25341c(bluetoothGattCharacteristic.getUuid()));
                this.c_condGattOperationComplete.await(GATT_OPERATION_TIMEOUT, TimeUnit.SECONDS);
                if (this.c_isGattOperationLocking) {
                    GuliLog.WARN("GATT OPERATION TIMEOUT");
                    GuliLog.m12359b("CONTINUE: synchronizedWriteCharacteristic(): " + GattUtils.m25341c(bluetoothGattCharacteristic.getUuid()));
                    break;
                }
                GuliLog.m12359b("CONTINUE: synchronizedWriteCharacteristic(): " + GattUtils.m25341c(bluetoothGattCharacteristic.getUuid()));
            }
            this.c_GattOperationLock.unlock();
        } catch (InterruptedException e) {
            e.printStackTrace();
            this.c_GattOperationLock.unlock();
        } catch (Throwable th) {
            this.c_GattOperationLock.unlock();
        }
        return this.f19502o;
    }

    class C3880m implements Runnable {
        final /* synthetic */ BluetoothGatt f19537a;
        final /* synthetic */ BluetoothGattDescriptor f19538b;
        final /* synthetic */ AbsGattCallback f19539c;

        C3880m(AbsGattCallback c3868a, BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor) {
            this.f19539c = c3868a;
            this.f19537a = bluetoothGatt;
            this.f19538b = bluetoothGattDescriptor;
        }

        public void run() {
            GuliLog.m12371e();
            GuliLog.ASSERTION_RUN_ON_THREAD(this.f19539c.c_GattQueueThread);
            GuliLog.ASSERT_NOT_NULL(this.f19537a);
            if (this.f19537a == null) {
                this.f19539c.c_GattOperationLock.lock();
                try {
                    this.f19539c.f19502o = C3867z.f19487h;
                    GuliLog.m12359b("NOTIFY: synchronizedReadDescriptor(): " + this.f19539c.f19502o);
                    this.f19539c.c_isGattOperationLocking = false;
                    this.f19539c.c_condGattOperationComplete.signal();
                } finally {
                    this.f19539c.c_GattOperationLock.unlock();
                }
            } else {
                boolean readDescriptor = this.f19537a.readDescriptor(this.f19538b);
                GuliLog.ASSERTION_TRUE(readDescriptor);
                if (!readDescriptor) {
                    this.f19539c.c_GattOperationLock.lock();
                    try {
                        this.f19539c.f19502o = C3867z.f19488i;
                        GuliLog.m12359b("NOTIFY: synchronizedReadDescriptor(): " + this.f19539c.f19502o);
                        this.f19539c.c_isGattOperationLocking = false;
                        this.f19539c.c_condGattOperationComplete.signal();
                    } finally {
                        this.f19539c.c_GattOperationLock.unlock();
                    }
                }
            }
        }
    }

    private synchronized int synchronizedReadDescriptor(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor) { // m25247a
        GuliLog.m12371e();
        GuliLog.m12350a("gatt=" + bluetoothGatt + ", characteristic=" + bluetoothGattDescriptor);
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGatt);
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGattDescriptor);
        Runnable c3880m = new C3880m(this, bluetoothGatt, bluetoothGattDescriptor);
        this.c_GattOperationLock.lock();
        try {
            this.c_isGattOperationLocking = true;
            this.c_GattQueue.post(c3880m);
            while (this.c_isGattOperationLocking) {
                if (!isConnected(bluetoothGatt)) {
                    GuliLog.WARN("NO CONNECTION");
                    break;
                }
                GuliLog.m12359b("WAIT: synchronizedReadDescriptor(): " + GattUtils.m25341c(bluetoothGattDescriptor.getUuid()));
                this.c_condGattOperationComplete.await(GATT_OPERATION_TIMEOUT, TimeUnit.SECONDS);
                if (this.c_isGattOperationLocking) {
                    GuliLog.WARN("GATT OPERATION TIMEOUT");
                    GuliLog.m12359b("CONTINUE: synchronizedReadDescriptor(): " + GattUtils.m25341c(bluetoothGattDescriptor.getUuid()));
                    break;
                }
                GuliLog.m12359b("CONTINUE: synchronizedReadDescriptor(): " + GattUtils.m25341c(bluetoothGattDescriptor.getUuid()));
            }
            this.c_GattOperationLock.unlock();
        } catch (InterruptedException e) {
            e.printStackTrace();
            this.c_GattOperationLock.unlock();
        } catch (Throwable th) {
            this.c_GattOperationLock.unlock();
        }
        return this.f19502o;
    }

    class C3883p implements Runnable {
        final /* synthetic */ BluetoothGatt f19542a;
        final /* synthetic */ BluetoothGattDescriptor f19543b;
        final /* synthetic */ byte[] f19544c;
        final /* synthetic */ AbsGattCallback f19545d;

        C3883p(AbsGattCallback c3868a, BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, byte[] bArr) {
            this.f19545d = c3868a;
            this.f19542a = bluetoothGatt;
            this.f19543b = bluetoothGattDescriptor;
            this.f19544c = bArr;
        }

        public void run() {
            GuliLog.m12371e();
            GuliLog.ASSERTION_RUN_ON_THREAD(this.f19545d.c_GattQueueThread);
            GuliLog.ASSERT_NOT_NULL(this.f19542a);
            if (this.f19542a == null) {
                this.f19545d.c_GattOperationLock.lock();
                try {
                    this.f19545d.f19502o = C3867z.f19487h;
                    GuliLog.m12359b("NOTIFY: synchronizedWriteDescriptor(): " + this.f19545d.f19502o);
                    this.f19545d.c_isGattOperationLocking = false;
                    this.f19545d.c_condGattOperationComplete.signal();
                } finally {
                    this.f19545d.c_GattOperationLock.unlock();
                }
            } else {
                this.f19543b.setValue(this.f19544c);
                boolean writeDescriptor = this.f19542a.writeDescriptor(this.f19543b);
                GuliLog.ASSERTION_TRUE(writeDescriptor);
                if (!writeDescriptor) {
                    this.f19545d.c_GattOperationLock.lock();
                    try {
                        this.f19545d.f19502o = C3867z.f19488i;
                        GuliLog.m12359b("NOTIFY: synchronizedWriteDescriptor(): " + this.f19545d.f19502o);
                        this.f19545d.c_isGattOperationLocking = false;
                        this.f19545d.c_condGattOperationComplete.signal();
                    } finally {
                        this.f19545d.c_GattOperationLock.unlock();
                    }
                }
            }
        }
    }

    private synchronized int synchronizedWriteDescriptor(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, byte[] bArr) { // m25248a
        GuliLog.m12371e();
        GuliLog.m12350a("gatt=" + bluetoothGatt + ", characteristic=" + bluetoothGattDescriptor + ", value=" + bArr);
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGatt);
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGattDescriptor);
        GuliLog.ASSERT_NOT_NULL((Object) bArr);
        Runnable c3883p = new C3883p(this, bluetoothGatt, bluetoothGattDescriptor, bArr);
        this.c_GattOperationLock.lock();
        try {
            this.c_isGattOperationLocking = true;
            this.c_GattQueue.post(c3883p);
            while (this.c_isGattOperationLocking) {
                if (!isConnected(bluetoothGatt)) {
                    GuliLog.WARN("NO CONNECTION");
                    break;
                }
                GuliLog.m12359b("WAIT: synchronizedWriteDescriptor(): " + GattUtils.m25341c(bluetoothGattDescriptor.getUuid()));
                this.c_condGattOperationComplete.await(GATT_OPERATION_TIMEOUT, TimeUnit.SECONDS);
                if (this.c_isGattOperationLocking) {
                    GuliLog.WARN("GATT OPERATION TIMEOUT");
                    GuliLog.m12359b("CONTINUE: synchronizedWriteDescriptor(): " + GattUtils.m25341c(bluetoothGattDescriptor.getUuid()));
                    break;
                }
                GuliLog.m12359b("CONTINUE: synchronizedWriteDescriptor(): " + GattUtils.m25341c(bluetoothGattDescriptor.getUuid()));
            }
            this.c_GattOperationLock.unlock();
        } catch (InterruptedException e) {
            e.printStackTrace();
            this.c_GattOperationLock.unlock();
        } catch (Throwable th) {
            this.c_GattOperationLock.unlock();
        }
        return this.f19502o;
    }

    private void printClientIf(BluetoothGatt bluetoothGatt) { // m25252a
        GuliLog.m12350a("mClientIf = " + GattUtils.printClientIf(bluetoothGatt));
    }

    private boolean isConnected(BluetoothGatt bluetoothGatt) { // m25255b
        return bluetoothGatt != null;
    }

    public final int readCharacteristic(BluetoothGattCharacteristic bluetoothGattCharacteristic) { // m25260a
        return synchronizedReadCharacteristic(this.f19512y, bluetoothGattCharacteristic);
    }

    public final int writeCharacteristic(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) { // m25261a
        return synchronizedWriteCharacteristic(this.f19512y, bluetoothGattCharacteristic, bArr);
    }

    public final BluetoothGattService getService(UUID uuid) { // m25262a
        GuliLog.m12371e();
        GuliLog.ASSERT_NOT_NULL(this.f19512y);
        return this.f19512y == null ? null : this.f19512y.getService(uuid);
    }

    class C3884q implements Runnable {
        final /* synthetic */ AbsGattCallback f19546a;

        C3884q(AbsGattCallback c3868a) {
            this.f19546a = c3868a;
        }

        public void run() {
            this.f19546a.connectInternal();
        }
    }

    public final void m25263a() {
        GuliLog.m12371e();
        this.f19511x.execute(new C3884q(this));
    }

    protected  void GattCB_onReadRemoteRssi(int i) {}

    public final void m25265a(BluetoothGattCharacteristic bluetoothGattCharacteristic, INotifyCallback c3838b) {
        if (!this.m_NotifyCBs.containsKey(bluetoothGattCharacteristic)) {
            this.m_NotifyCBs.put(bluetoothGattCharacteristic, c3838b);
        }
    }

    protected  void GattCB_onDescriptorRead(BluetoothGattDescriptor bluetoothGattDescriptor) {}

    class C3885r implements Runnable {
        final /* synthetic */ AbsGattCallback f19547a;

        C3885r(AbsGattCallback c3868a) {
            this.f19547a = c3868a;
        }

        public void run() {
            this.f19547a.disconnectInternal();
        }
    }

    public final void m25267b() {
        GuliLog.m12371e();
        this.f19511x.execute(new C3885r(this));
    }

    public final void m25268b(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        this.m_NotifyCBs.remove(bluetoothGattCharacteristic);
    }

    protected  void GattCB_onDescriptorWrite(BluetoothGattDescriptor bluetoothGattDescriptor) {} // m25269b

    public final boolean registerNotification(BluetoothGattCharacteristic bluetoothGattCharacteristic, INotifyCallback c3838b) { // m25270b
        boolean z = true;
        GuliLog.m12371e();
        if (c3838b != null) {
            this.m_NotifyCBs.put(bluetoothGattCharacteristic, c3838b);
        }
        if (this.f19512y == null) {
            return false;
        }
        boolean characteristicNotification = this.f19512y.setCharacteristicNotification(bluetoothGattCharacteristic, true);
        GuliLog.ASSERTION_TRUE(characteristicNotification);
        if (!characteristicNotification) {
            return false;
        }
        BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(GattUtils.f19564a);
        GuliLog.ASSERT_NOT_NULL((Object) descriptor);
        if (descriptor == null) {
            return false;
        }
        if ((bluetoothGattCharacteristic.getProperties() & 16) > 0) {
            if (synchronizedWriteDescriptor(this.f19512y, descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) != 0) {
                z = false;
            }
            return z;
        } else if ((bluetoothGattCharacteristic.getProperties() & 32) <= 0) {
            return false;
        } else {
            if (synchronizedWriteDescriptor(this.f19512y, descriptor, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE) != 0) {
                z = false;
            }
            return z;
        }
    }

    public void m25271c() {
        try {
            BluetoothGatt.class.getDeclaredMethod("refresh", (Class[]) null).invoke(this.f19512y, (Object[]) null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public final boolean unregisterNotification(BluetoothGattCharacteristic bluetoothGattCharacteristic) { // m25272c
        boolean z = true;
        boolean characteristicNotification;
        GuliLog.m12371e();
        this.m_NotifyCBs.remove(bluetoothGattCharacteristic);
        if (this.mConState != XMBLEMSTATUS.CONNECTED) {
            GuliLog.m12350a("m_State != STATE_CONNECTED");
            if (this.f19512y != null) {

                GuliLog.m12350a("m_Gatt != null, trying to unregister locally...");
                characteristicNotification = this.f19512y.setCharacteristicNotification(bluetoothGattCharacteristic, false);
                GuliLog.ASSERTION_TRUE(characteristicNotification);
                if (!characteristicNotification) {
                    return false;
                }
            }
            return true;
        }
        GuliLog.ASSERT_NOT_NULL(this.f19512y);
        characteristicNotification = this.f19512y.setCharacteristicNotification(bluetoothGattCharacteristic, false);
        GuliLog.ASSERTION_TRUE(characteristicNotification);
        if (!characteristicNotification) {
            return false;
        }
        BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(GattUtils.f19564a);
        GuliLog.ASSERT_NOT_NULL((Object) descriptor);
        if (descriptor == null) {
            return false;
        }
        if (synchronizedWriteDescriptor(this.f19512y, descriptor, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE) != 0) {
            z = false;
        }
        return z;
    }

    public final List<BluetoothGattService> getServices() { // m25273d
        GuliLog.m12371e();
        GuliLog.ASSERT_NOT_NULL(this.f19512y);
        return this.f19512y == null ? null : this.f19512y.getServices();
    }

    protected  void GattCB_onCharacteristicChanged(BluetoothGattCharacteristic bluetoothGattCharacteristic) {}

    public final int readRemoteRSSI() { // m25275e
        int i = 0;
        GuliLog.m12371e();
        this.c_isRSSILocking = true;
        GuliLog.ASSERT_NOT_NULL(this.f19512y);
        if (this.f19512y == null) {
            this.c_isRSSILocking = false;
        } else if (this.f19512y.readRemoteRssi()) {
            synchronized (this.f19503p) {
                while (this.c_isRSSILocking) {
                    try {
                        GuliLog.m12359b("WAIT: readRemoteRSSI()");
                        this.f19503p.wait(GATT_OPERATION_TIMEOUT);
                        if (this.c_isRSSILocking) {
                            GuliLog.WARN("GATT OPERATION TIMEOUT");
                            GuliLog.m12359b("CONTINUE: readRemoteRSSI()");
                            break;
                        }
                        GuliLog.m12359b("CONTINUE: readRemoteRSSI()");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (this.f19506s != 0) {
                } else {
                    i = this.f19505r;
                }
            }
        } else {
            this.c_isRSSILocking = false;
        }
        return i;
    }

    protected  void GattCB_onCharacteristicRead(BluetoothGattCharacteristic bluetoothGattCharacteristic) {}

    public final BluetoothDevice m25277f() {
        return this.f19508u;
    }

    protected  void GattCB_onCharacteristicWrite(BluetoothGattCharacteristic bluetoothGattCharacteristic) {}

    public final XMBLEMSTATUS m25279g() {
        return this.mConState;
    }

    protected  void GattCB_onConnected() {}

    protected  void GattCB_onDisconnected() {}

    protected  void GattCB_onConnectionFailed(){}

    protected  void GattCB_onServiceDiscovered(){}

    protected final BluetoothGatt m25284l() {
        return this.f19512y;
    }

    protected void connectInternal() {
        GuliLog.m12371e();
        GuliLog.m12350a("m_State: " + this.mConState);
        if (this.mConState != XMBLEMSTATUS.DISCONNECTED) {
            GuliLog.WARN("Illegal state: m_State = " + this.mConState);
        }
        this.mConState = XMBLEMSTATUS.CONNECTING;
        if (this.f19512y != null) {
            GuliLog.m12350a("connect back...");
        }
        this.m_isClean = false;
        // to set a timer for the timeout of connecting, the current value is 45 seconds.
        this.c_GattQueue.postDelayed(this.f19510w, (long) this.f19509v);

        // connect to the gatt
        this.f19512y = this.f19508u.connectGatt(this.f19507t, false, this);
        if (this.f19512y == null) {
            GuliLog.m12370d("xxx", "connectGatt return null, now disable bluetooth!!!");
            ((BLEService) this.f19507t).m25143e();
        }
        GuliLog.ASSERT_NOT_NULL(this.f19512y);
    }

    protected void disconnectInternal() { // m25286n
        GuliLog.m12371e();
        GuliLog.m12350a("m_State: " + this.mConState);
        if (this.mConState == XMBLEMSTATUS.CONNECTING || this.mConState == XMBLEMSTATUS.CONNECTED) {
            GuliLog.ASSERT_NOT_NULL(this.f19512y);
            this.mConState = XMBLEMSTATUS.DISCONNECTING;
            this.f19512y.disconnect();
            return;
        }
        GuliLog.WARN("Illegal state: m_State != STATE_CONNECTING && m_State != STATE_CONNECTED");
    }

    protected void cleanupInternal() { // m25287o
        GuliLog.m12371e();
        GuliLog.m12350a("m_State: " + this.mConState);
        if (this.mConState != XMBLEMSTATUS.DISCONNECTED) {
            GuliLog.WARN("Illegal state: m_State = " + this.mConState);
            return;
        }
        this.m_NotifyCBs.clear();
        this.c_GattOperationLock.lock();
        try {
            this.c_isGattOperationLocking = false;
            this.f19502o = C3867z.f19489j;
            GuliLog.m12359b("RELEASE: c_condGattOperationComplete");
            this.c_condGattOperationComplete.signalAll();
            synchronized (this.f19503p) {
                this.c_isRSSILocking = false;
                this.f19505r = 0;
                this.f19506s = C3867z.f19489j;
                GuliLog.m12359b("RELEASE: c_RSSILock");
                this.f19503p.notifyAll();
            }
            this.f19512y.close();
            this.f19512y = null;
            this.m_isClean = true;
        } finally {
            this.c_GattOperationLock.unlock();
        }
    }

    public final void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        class C3872e implements Runnable {
            final /* synthetic */ INotifyCallback f19518a;
            final /* synthetic */ byte[] f19519b;
            final /* synthetic */ AbsGattCallback f19520c;

            C3872e(AbsGattCallback c3868a, INotifyCallback c3838b, byte[] bArr) {
                this.f19520c = c3868a;
                this.f19518a = c3838b;
                this.f19519b = bArr;
            }

            public void run() {
                this.f19518a.notify(this.f19519b);
            }
        }
        GuliLog.m12371e();
        GuliLog.ASSERTION_TRUE(this.f19512y == bluetoothGatt);
        byte[] value = bluetoothGattCharacteristic.getValue();
        GuliLog.m12350a("Characteristic Changed: " + bluetoothGattCharacteristic.getUuid());
        GuliLog.m12350a("Characteristic Changed: " + GattUtils.m25335b(value));
//        // TBD
//        Log.e("Characteristic Changed: ", "" + GattUtils.m25335b(value));
        Object obj = (INotifyCallback) this.m_NotifyCBs.get(bluetoothGattCharacteristic);
        GuliLog.ASSERT_NOT_NULL(obj);
        this.f19511x.execute(new C3872e(this, (INotifyCallback) obj, value));

        class C3873f implements Runnable {
            final /* synthetic */ BluetoothGattCharacteristic f19521a;
            final /* synthetic */ AbsGattCallback f19522b;

            C3873f(AbsGattCallback c3868a, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
                this.f19522b = c3868a;
                this.f19521a = bluetoothGattCharacteristic;
            }

            public void run() {
                this.f19522b.GattCB_onCharacteristicChanged(this.f19521a);
            }
        }
        this.f19511x.execute(new C3873f(this, bluetoothGattCharacteristic));
    }

    class C3890w implements Runnable {
        final /* synthetic */ BluetoothGattCharacteristic f19552a;
        final /* synthetic */ AbsGattCallback f19553b;

        C3890w(AbsGattCallback c3868a, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            this.f19553b = c3868a;
            this.f19552a = bluetoothGattCharacteristic;
        }

        public void run() {
            this.f19553b.GattCB_onCharacteristicRead(this.f19552a);
        }
    }

    public final void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
        boolean z = true;
        GuliLog.m12371e();
        GuliLog.ASSERTION_TRUE(i == 0);
        if (this.f19512y != bluetoothGatt) {
            z = false;
        }
        GuliLog.ASSERTION_TRUE(z);
        GuliLog.m12350a("Characteristic Read: " + GattUtils.m25335b(bluetoothGattCharacteristic.getValue()));
        this.c_GattOperationLock.lock();
        try {
            this.f19502o = i;
            GuliLog.m12359b("NOTIFY: onCharacteristicRead(): " + this.f19502o);
            this.c_isGattOperationLocking = false;
            this.c_condGattOperationComplete.signal();
            if (i == 0) {
                this.f19511x.execute(new C3890w(this, bluetoothGattCharacteristic));
            }
        } finally {
            this.c_GattOperationLock.unlock();
        }
    }

    class C3871d implements Runnable {
        final /* synthetic */ BluetoothGattCharacteristic f19516a;
        final /* synthetic */ AbsGattCallback f19517b;

        C3871d(AbsGattCallback c3868a, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            this.f19517b = c3868a;
            this.f19516a = bluetoothGattCharacteristic;
        }

        public void run() {
            this.f19517b.GattCB_onCharacteristicWrite(this.f19516a);
        }
    }

    public final void onCharacteristicWrite(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
        boolean z = true;
        GuliLog.m12371e();
        GuliLog.ASSERTION_TRUE(i == 0);
        if (this.f19512y != bluetoothGatt) {
            z = false;
        }
        GuliLog.ASSERTION_TRUE(z);
        GuliLog.m12350a("Characteristic Write: " + GattUtils.m25335b(bluetoothGattCharacteristic.getValue()));
        this.c_GattOperationLock.lock();
        try {
            this.f19502o = i;
            GuliLog.m12359b("NOTIFY: onCharacteristicWrite(): " + this.f19502o);
            this.c_isGattOperationLocking = false;
            this.c_condGattOperationComplete.signal();
            if (i == 0) {
                this.f19511x.execute(new C3871d(this, bluetoothGattCharacteristic));
            }
        } finally {
            this.c_GattOperationLock.unlock();
        }
    }

    public final void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i, int i2) {
        class C3888u implements Runnable {
            final /* synthetic */ AbsGattCallback f19550a;

            C3888u(AbsGattCallback c3868a) {
                this.f19550a = c3868a;
            }

            public void run() {
                this.f19550a.GattCB_onConnectionFailed();
            }
        }

        class C3887t implements Runnable {
            final /* synthetic */ AbsGattCallback f19549a;

            C3887t(AbsGattCallback c3868a) {
                this.f19549a = c3868a;
            }

            public void run() {
                this.f19549a.GattCB_onDisconnected();
            }
        }

        class C3886s implements Runnable {
            final /* synthetic */ AbsGattCallback f19548a;

            C3886s(AbsGattCallback c3868a) {
                this.f19548a = c3868a;
            }

            public void run() {
                this.f19548a.GattCB_onConnected();
            }
        }

        GuliLog.m12370d(TAG, "onConnectionStateChange:m_Gatt:" + this.f19512y + ",gatt:" + bluetoothGatt + ",status:" + i + ",newState:" + i2);
        this.c_GattQueue.removeCallbacks(this.f19510w);
        GuliLog.m12371e();
        GuliLog.ASSERTION_TRUE(i == 0);
        if (this.f19512y != bluetoothGatt) {
            GuliLog.WARN("m_Gatt: " + this.f19512y);
            GuliLog.WARN("  gatt: " + bluetoothGatt);
            if (this.f19512y != null) {
                GuliLog.WARN("m_Gatt.getDevice(): " + this.f19512y.getDevice());
                printClientIf(this.f19512y);
            }
            if (bluetoothGatt != null) {
                GuliLog.WARN("  gatt.getDevice(): " + bluetoothGatt.getDevice());
                printClientIf(bluetoothGatt);
            }
            this.f19512y = bluetoothGatt;
        }
        if (i == 0) {
            switch (i2) {
                case C4220a.f20886i /*0*/:
                    this.mConState = XMBLEMSTATUS.DISCONNECTED;
                    GuliLog.INFO("=================================================");
                    GuliLog.INFO("================== DISCONNECTED =================");
                    GuliLog.INFO("=================================================");
                    this.f19511x.execute(new C3887t(this));
                    return;
                case C4220a.f20888k /*2*/:
                    this.mConState = XMBLEMSTATUS.CONNECTED;
                    GuliLog.INFO("=================================================");
                    GuliLog.INFO("=================== CONNECTED ===================");
                    GuliLog.INFO("=================================================");
                    printClientIf(this.f19512y);
                    this.f19511x.execute(new C3886s(this));
                    return;
                default:
                    GuliLog.WARN(">>> UNEXPECTED <<<");
                    return;
            }
        }
        GuliLog.WARN("m_Gatt: " + this.f19512y + ",status:" + i);
        GuliLog.WARN("  gatt: " + bluetoothGatt);
        if (this.f19512y != null) {
            GuliLog.WARN("m_Gatt.getDevice(): " + (this.f19512y != null ? this.f19512y.getDevice() : C3657a.f18328f));
            printClientIf(this.f19512y);
        }
        if (bluetoothGatt != null) {
            GuliLog.WARN("  gatt.getDevice(): " + (bluetoothGatt != null ? bluetoothGatt.getDevice() : C3657a.f18328f));
            printClientIf(bluetoothGatt);
        }
        this.mConState = XMBLEMSTATUS.DISCONNECTED;
        GuliLog.ERROR("=================================================");
        GuliLog.ERROR("=============== CONNECTION FAILED ===============");
        GuliLog.ERROR("=================================================");
        this.f19511x.execute(new C3888u(this));
        int a = GattUtils.printClientIf(this.f19512y);
        if (a > 10 || a == 0) {
            GuliLog.m12370d(TAG, "clientIf is " + a + ", now disable bluetooth!!!");
            ((BLEService) this.f19507t).m25143e();
        }
    }

    public final void onDescriptorRead(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i) {
        class C3874g implements Runnable {
            final /* synthetic */ BluetoothGattDescriptor f19523a;
            final /* synthetic */ AbsGattCallback f19524b;

            C3874g(AbsGattCallback c3868a, BluetoothGattDescriptor bluetoothGattDescriptor) {
                this.f19524b = c3868a;
                this.f19523a = bluetoothGattDescriptor;
            }

            public void run() {
                this.f19524b.GattCB_onDescriptorRead(this.f19523a);
            }
        }

        boolean z = true;
        GuliLog.m12371e();
        GuliLog.ASSERTION_TRUE(i == 0);
        if (this.f19512y != bluetoothGatt) {
            z = false;
        }
        GuliLog.ASSERTION_TRUE(z);
        GuliLog.m12350a("Descriptor Read: " + GattUtils.m25335b(bluetoothGattDescriptor.getValue()));
        this.c_GattOperationLock.lock();
        try {
            this.f19502o = i;
            GuliLog.m12359b("NOTIFY: onDescriptorRead(): " + this.f19502o);
            this.c_isGattOperationLocking = false;
            this.c_condGattOperationComplete.signal();
            if (i == 0) {
                this.f19511x.execute(new C3874g(this, bluetoothGattDescriptor));
            }
        } finally {
            this.c_GattOperationLock.unlock();
        }
    }

    public final void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i) {
        class C3875h implements Runnable {
            final /* synthetic */ BluetoothGattDescriptor f19525a;
            final /* synthetic */ AbsGattCallback f19526b;

            C3875h(AbsGattCallback c3868a, BluetoothGattDescriptor bluetoothGattDescriptor) {
                this.f19526b = c3868a;
                this.f19525a = bluetoothGattDescriptor;
            }

            public void run() {
                this.f19526b.GattCB_onDescriptorWrite(this.f19525a);
            }
        }

        boolean z = true;
        GuliLog.m12371e();
        GuliLog.ASSERTION_TRUE(i == 0);
        if (this.f19512y != bluetoothGatt) {
            z = false;
        }
        GuliLog.ASSERTION_TRUE(z);
        GuliLog.m12350a("Descriptor Write: " + GattUtils.m25335b(bluetoothGattDescriptor.getValue()));
        this.c_GattOperationLock.lock();
        try {
            this.f19502o = i;
            GuliLog.m12359b("NOTIFY: onDescriptorWrite(): " + this.f19502o);
            this.c_isGattOperationLocking = false;
            this.c_condGattOperationComplete.signal();
            if (i == 0) {
                this.f19511x.execute(new C3875h(this, bluetoothGattDescriptor));
            }
        } finally {
            this.c_GattOperationLock.unlock();
        }
    }

    public final void onReadRemoteRssi(BluetoothGatt bluetoothGatt, int i, int i2) {
        class C3876i implements Runnable {
            final /* synthetic */ int f19527a;
            final /* synthetic */ AbsGattCallback f19528b;

            C3876i(AbsGattCallback c3868a, int i) {
                this.f19528b = c3868a;
                this.f19527a = i;
            }

            public void run() {
                this.f19528b.GattCB_onReadRemoteRssi(this.f19527a);
            }
        }

        boolean z = true;
        GuliLog.m12371e();
        GuliLog.ASSERTION_TRUE(i2 == 0);
        if (this.f19512y != bluetoothGatt) {
            z = false;
        }
        GuliLog.ASSERTION_TRUE(z);
        GuliLog.m12350a("RSSI: " + i);
        synchronized (this.f19503p) {
            this.f19505r = i;
            this.f19506s = i2;
            GuliLog.m12359b("NOTIFY: onReadRemoteRssi(): " + this.f19506s);
            this.c_isRSSILocking = false;
            this.f19503p.notify();
        }
        if (i2 == 0) {
            this.f19511x.execute(new C3876i(this, i));
        }
    }

    public final void onReliableWriteCompleted(BluetoothGatt bluetoothGatt, int i) {
        boolean z = true;
        GuliLog.m12371e();
        GuliLog.ASSERTION_TRUE(i == 0);
        if (this.f19512y != bluetoothGatt) {
            z = false;
        }
        GuliLog.ASSERTION_TRUE(z);
        GuliLog.WARN(">>> UNEXPECTED <<<");
    }

    public final void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i) {
        boolean z = true;
        class C3889v implements Runnable {
            final /* synthetic */ AbsGattCallback f19551a;

            C3889v(AbsGattCallback c3868a) {
                this.f19551a = c3868a;
            }

            public void run() {
                this.f19551a.GattCB_onServiceDiscovered();
            }
        }

        GuliLog.m12370d(TAG, "onServicesDiscovered:m_Gatt:" + this.f19512y + ",gatt:" + bluetoothGatt + ",status:" + i);
        GuliLog.m12371e();
        GuliLog.ASSERTION_TRUE(i == 0);
        if (this.f19512y != bluetoothGatt) {
            z = false;
        }
        GuliLog.ASSERTION_TRUE(z);
        if (i == 0) {
            this.f19511x.execute(new C3889v(this));
        } else {
            disconnectInternal();
        }
    }
}

